/*
 * Copyright 2008-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.app.jQuery.user.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.jQuery.user.domain.User;
import com.app.jQuery.user.service.UserService;

/**
 * 用户控制器
 * 
 * @author 李长伟
 * @version 1.0 创建时间 2015年6月17日 上午11:16:32
 * 
 */
@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 用户服务类
     */
    @Autowired
    private UserService userService;

    @RequestMapping("/toFindAll")
    public String toFindAll() {
        return "/index.html";
    }
    
    /**
     * 分页展示用户列表
     * 
     * @param pageNo 需要跳转的页码(从0开始)
     * @param pageSize 每页显示记录数
     * @return 返回符合条件的用户列表和分页参数
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam("offset") Integer pageNo,
            @RequestParam("limit") Integer pageSize) throws Exception {
        Assert.state(pageNo >= 0, "页码必须大于0");
        Assert.state(pageSize >= 10, "每页至少显示10条记录");
        Page<User> list = userService.findAll(new PageRequest(pageNo, pageSize));
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("total", list.getTotalElements());
        resultMap.put("rows", list.getContent());
        return resultMap;
    }
}
